###############################################################
# Clase Answer
# representa una respuesta al cliente. Contien un código HTTP y
# el contenido de HTML a enviar
###############################################################
class Answer

	def initialize()
		@HTTP_code = ""
		@content = ""
		@content_type = {
			
			#text
			'html' => 'text/html',
			'txt' => 'text/plain',
			'htmls' => 'text/html',
			'xml' => 'text/xml',
			'css' => 'text/css',
			'csv' => 'text/csv',
			'htmls' => 'text/html',
			
			#image
			'ico' => 'image/vnd.microsoft.icon',
			'png' => 'image/',
			'jpg' => 'image/jpg',
			'jpeg' => 'image/jpeg',
			'gif' => 'image/gif',
			'svg' => 'image/svg+xml',
			'tiff' => 'image/tiff',
			
			#audio
			'wav' => 'audio/vnd.wave',
			'wma' => 'audio/x-ms-wma',
			
			#video
			'mpeg' => 'video/mpeg',
			'mp4' => 'video/mp4',
			'wmv' => 'video/x-ms-wmv',
			
			#application
			'pdf' => 'application/pdf',
			'ogg' => 'application/ogg',
			'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
			'odt' => 'application/vnd.oasis.opendocument.text',
			'odp' => 'application/vnd.oasis.opendocument.presentation',
			#---------------------------------------
			'xls' => 'application/vnd.ms-excel',
			'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
			'doc' => 'application/msword',
			'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
			'ppt' => 'application/vnd.ms-powerpoint',
			'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
			
			
		}
	end
	#------------------------------------------------------
	# retorna ya sea @HTTP_code  o @content
	#------------------------------------------------------
	attr_reader :HTTP_code, :content, :authentication
	attr_writer :HTTP_code, :content, :authentication
	
	def setHTTP_code(code,ext)
		case code.downcase

			
		when "ok"
			
			@HTTP_code = "HTTP/1.1 200 OK"+"\n"
			@HTTP_code << "Server: miniServer/0.01 (Unix) (Debian/Linux)" << "\n"
			@HTTP_code << ("Date: " + Time.now.asctime + "\n")
			@HTTP_code << "Content-type: #{getContentType(ext)}" + "\n"
			@HTTP_code << "Content-Length: #{@content.length}" 


		when "not found"
			
			@content = errorPage("404 Not Found.")

			@HTTP_code = "HTTP/1.1 404 Not Found"+"\n"
			@HTTP_code << "Server: miniServer/0.01 (Unix) (Debian/Linux)" << "\n"
			@HTTP_code << ("Date: " + Time.now.asctime + "\n")
			@HTTP_code << "Content-type: text/html" << "\n"
			@HTTP_code << "Content-Length: #{@content.length}"
			

		when "folder found"
			
			@content = errorPage("404 Not Found. A folder was found")

			@HTTP_code = "HTTP/1.1 404 Not Found"+"\n"
			@HTTP_code << "Server: miniServer/0.01 (Unix) (Debian/Linux)" << "\n"
			@HTTP_code << ("Date: " + Time.now.asctime + "\n")
			@HTTP_code << "Content-type: text/html" << "\n"
			@HTTP_code << "Content-Length: #{@content.length}"
			
			 

		when "authorization required"
			
			
			@content = errorPage("401 Unauthorized.")
			
			@HTTP_code = "HTTP/1.1 401 Authorization Required"+"\n"
			@HTTP_code << "Server: HTTPd/1.0" << "\n"
			@HTTP_code << "Date: " + Time.now.asctime + "\n"
			@HTTP_code << "WWW-Authenticate: Basic realm=\"Secure Area\"" << "\n"
			@HTTP_code << "Content-Type: text/html" << "\n"
			@HTTP_code << "Content-Length: #{@content.length}"
			
			
			
		when "bad request"
			
			@content = errorPage("400 Bad Request.")
			
			@HTTP_code = "HTTP/1.1 400 Bad Request"+"\n"
			@HTTP_code << ("Date: " + Time.now.asctime + "\n")
			@HTTP_code << "Content-Type: text/html" << "\n"
			@HTTP_code << "Content-Length: #{@content.length}"

			
		end
	end
	
	#------------------------------------------------------
	# retorna una página html con una plantilla de error
	#------------------------------------------------------
	def errorPage(msj)
		temp = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""+"\n"
		temp << " \"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">"+"\n"
		temp << "<HTML>"+"\n"
		temp << "  <HEAD>"+"\n"
		temp << "    <TITLE>Error</TITLE>"+"\n"
		temp << "    <META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=ISO-8859-1\">"+"\n"
		temp << "  </HEAD>"+"\n"
		temp << "  <BODY><H1>#{msj}</H1></BODY>"+"\n"
		temp << "</HTML>"+"\n"
		return temp
	end
	
	#------------------------------------------------------
	# retorna el tipo del retorno
	#-----------------------------------------------------
	def getContentType(ext)
		if(@content_type.has_key?(ext.downcase))
		
			return @content_type[ext.downcase]
			
		end
		
		
		return "text/plain"
	end
	
	#------------------------------------------------------
	# retorna la respuesta completa
	#------------------------------------------------------
	def answer()
		var = "#{@HTTP_code}" + "\n"
		var << "\n"
		var << @content
		return var
	end
end
